import { registerOnHeartBeat } from "$lib/content/heart";
import { Types, defineComponent, type IWorld, type Entity } from "$lib/ecs";

export const Stamina = defineComponent('Stamina', {
  max: Types.i32,
  amount: Types.i32,
});

export const StaminaRegen = defineComponent('StaminaRegen', {
  amount: Types.i32,
});

export const getStamina = (world: IWorld, e: Entity) => {
  return {
    max: Stamina.max[e],
    amount: Stamina.amount[e],
  };
};

export const setStamina = (world: IWorld, e: Entity, amount: number) => {
  Stamina.amount[e] = Math.min(Stamina.max[e], Math.max(amount, 0));
};

export const getStaminaRegen = (world: IWorld, e: Entity) => {
  // could be affected by buffs later on
  return StaminaRegen.amount[e];
};

registerOnHeartBeat(StaminaRegen, (world, e) => {
  const regen = getStaminaRegen(world, e);
  const { amount } = getStamina(world, e);
  setStamina(world, e, amount+regen);
});
